package com.scau.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.scau.model.Employee;
import com.scau.model.Product;
import com.scau.model.Salary;
import com.scau.service.SalaryService;


@Component
@Scope("prototype")
public class SalaryAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SalaryService salaryService;
	
	public String list(){
		List<Salary> list=salaryService.listAll(new Salary());
		httpServletRequest.setAttribute("salaryList", list);
		return "list";
	}
	
	public String add() {
		List<Salary> list=new ArrayList<Salary>();
		return "add";
	}
	
	public String save() {
		double expend=Double.parseDouble(httpServletRequest.getParameter("expend"));
		double balance=Double.parseDouble(httpServletRequest.getParameter("balance"));
		String userid=httpServletRequest.getParameter("userid");
		Salary salary =new Salary();
		salary.setBalance(balance);
		salary.setDate(new Date());
		salary.setUserid(userid);
		salary.setExpend(expend);
		salaryService.add(salary);
		return "save";
	}
	
	public String checkbalance(){
		String userid=httpServletRequest.getParameter("userid");
		Salary salary=salaryService.findUniqueById(new Salary(), "userid",userid , "date");
		if(salary!=null){
		httpServletRequest.setAttribute("curbalance", salary.getBalance());
		}else {
			httpServletRequest.setAttribute("curbalance", 0);
		}
		
		return "curbalance";
	}

	public SalaryService getSalaryService() {
		return salaryService;
	}

	@Resource
	public void setSalaryService(SalaryService salaryService) {
		this.salaryService = salaryService;
	}
}
